package br.edu.ifpb.restaurante.classesDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.edu.ifpb.restaurante.conexao.ConnectionFactory;
import br.edu.ifpb.restaurante.entidades.Usuario;


public class UsuarioDAO implements DAO <Usuario> {
	
	private Connection connection;
	
	public UsuarioDAO(){
		connection = new ConnectionFactory().getConnection();
	}
	
	@Override
	public boolean insert(Usuario value) throws SQLException {
		
		try {
			String sql = "insert into tb_usuario (login,senha,nome) values (?,?,?)";
			
			PreparedStatement stmt = connection.prepareStatement(sql);
				
			stmt.setString(1, value.getLogin());
			stmt.setString(2, value.getSenha());
			stmt.setString(3, value.getNome());
			
			stmt.execute();
			
			return true;
		
		} catch (SQLException e){
			return false;
		}
	
	}

	@Override
	public ArrayList<Usuario> getAll() throws SQLException {
		
		ArrayList<Usuario> lista = new ArrayList<Usuario>();
		Usuario u;
		
		String sql = "select * from tb_usuario order by nome";
		PreparedStatement stmt = connection.prepareStatement(sql);			
		
		ResultSet rs = stmt.executeQuery();
		
		while (rs.next()){
			u = new Usuario();
			
			u.setId(rs.getInt("id_usuario"));
			u.setLogin(rs.getString("login"));
			u.setNome(rs.getString("nome"));
			
			lista.add(u);
		}
		
		return lista;
	}

	@Override
	public Usuario get(int id) throws SQLException {
		
		Usuario u = null;
		
		String sql = "select * tb_usuario where id_usuario = " + id;
		PreparedStatement stmt = connection.prepareStatement(sql);
		
		ResultSet rs = stmt.executeQuery();
		
		if (rs.next()){
			
			u = new Usuario();
			
			u.setId(id);
			u.setLogin(rs.getString("login"));
			u.setNome(rs.getString("nome"));
			u.setSenha(rs.getString("senha"));
			
		}
		
		return u;
	}

	@Override
	public boolean delete(int id) throws SQLException {
		
		try {
			
			String sql = "delete from tb_usuario where id_usuario = " + id;
			PreparedStatement stmt = connection.prepareStatement(sql);
			
			stmt.execute();
			
			return true;
			
		} catch (SQLException e){
			return false;
		}
	
	}

	@Override
	public void update(Usuario value) throws SQLException {
		
		String sql = "update tb_usuario set nome = ?, login = ?, senha = ? where id_usuario = ?";
		PreparedStatement stmt = connection.prepareStatement(sql);
		
		stmt.setString(1, value.getNome());
		stmt.setString(2, value.getLogin());
		stmt.setString(3, value.getSenha());
		stmt.setInt(4, value.getId());
		
		stmt.execute();
		
	}
	
	@Override
	public void disconnect() throws SQLException{
		this.connection.close();
	}
	
	public Usuario getAttributes(String login) throws SQLException{
		
		Usuario u = null;
		
		String sql = "select * from tb_usuario where login = '"+ login +"'";
		PreparedStatement stmt = connection.prepareStatement(sql);			
		
		ResultSet rs = stmt.executeQuery();
		
		if (rs.next()){
			u = new Usuario();
			
			u.setId(rs.getInt("id_usuario"));
			u.setLogin(rs.getString("login"));
			u.setSenha(rs.getString("senha"));
			u.setNome(rs.getString("nome"));
		}
		
		return u;
	}

}
